import io.appium.java_client.MobileElement;
import io.appium.java_client.android.AndroidDriver;
import org.openqa.selenium.By;
import org.openqa.selenium.remote.DesiredCapabilities;

import java.net.MalformedURLException;
import java.net.URL;

public class HuanxinTest {
    public static void main(String[] args) {
        DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities.setCapability("deviceName", "127.0.0.1:62001");
        capabilities.setCapability("platformName", "Android");
        capabilities.setCapability("appPackage", "com.cnezsoft.xxm");
        capabilities.setCapability("appActivity", "com.cnezsoft.xxm.ui.LauncherActivity");
        capabilities.setCapability("noReset", true);

        AndroidDriver<MobileElement> driver = null;

        try {
            driver = new AndroidDriver<>(new URL("http://0.0.0.0:4723/wd/hub"), capabilities);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }

        // 等待喧喧加载完成
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        // 进入“消息”界面
        MobileElement messageButton = driver.findElement(By.xpath("//android.widget.TextView[@text='消息']"));
        messageButton.click();

        // 打开“小喧喧”对话框
        MobileElement xiaoxuanxuanButton = driver.findElement(By.xpath("//android.widget.TextView[@text='小喧喧']"));
        xiaoxuanxuanButton.click();

        // 发送十条消息
        for (int i = 0; i < 10; i++) {
            MobileElement inputBox = driver.findElement(By.xpath("//android.widget.EditText[@text='请输入消息']"));
            inputBox.sendKeys("测试消息" + i);
            MobileElement sendButton = driver.findElement(By.xpath("//android.widget.TextView[@text='发送']"));
            sendButton.click();

            // 等待消息发送完成
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        // 关闭连接
        driver.quit();
    }
}